package br.org.bertol.mestrado.search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import br.org.bertol.mestrado.Verify;
import br.org.bertol.mestrado.optimisation.hillclimbing.Path;
import br.org.bertol.mestrado.optimisation.pso.Particle;

public class RandomSolution 
{
	private Verify verify;
	
	private List<Path> randomPaths;
	
	private List<Particle> randomParticles;
	
	public RandomSolution()
	{
	}
	
	public void setVerify(final Verify sa)
	{
		verify = sa;
	}
	
	@SuppressWarnings("unchecked")
	public List<Path> generateRandomSolutions(final int max_random_solutions)
	{
		if(randomPaths != null)
			return randomPaths;
		
		randomPaths = new ArrayList<Path>(max_random_solutions);
		
		for (int i = 0; i < max_random_solutions; i++)
		{
			final Path randomPath = new Path();
			
			randomPath.setRoute((ArrayList<Integer>)verify.getClasses().clone());
						
			while(randomPath.getHeigtht() == Integer.MAX_VALUE)
			{
				Collections.shuffle(randomPath.getRoute());
				
				verify.verifyStrongCsontraint(randomPath);
			}
			
			verify.verifyWeekConstraint(randomPath, true);
			
			randomPaths.add(randomPath);
		}
		
		return randomPaths;
	}
	
	@SuppressWarnings("unchecked")
	public List<Particle> generateRandomPSOSolutions(final int max_random_solutions)
	{
		if(randomParticles != null)
			return randomParticles;
		
		randomParticles = new ArrayList<Particle>(max_random_solutions);
		
		for (int i = 0; i < max_random_solutions; i++)
		{
			final Particle randomParticle = new Particle(verify.getClasses().size(), (float)0.5, (float)0.5, (float)0.5);
			
			randomParticle.setRoute((ArrayList<Integer>)verify.getClasses().clone());
						
			while(randomParticle.getHeigtht() == Integer.MAX_VALUE)
			{
				Collections.shuffle(randomParticle.getRoute());
				
				verify.verifyStrongCsontraint(randomParticle);
			}
			
			verify.verifyWeekConstraint(randomParticle, true);
			
			randomParticles.add(randomParticle);
		}
		
		return randomParticles;
	}
}

